{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Rust - Python Mixed Debugging Example\n",
    "\n",
    "This notebook demonstrates mixed Python + Rust debugging.  \n",
    "You need to execute this notebook from within VSCode.  \n",
    "See the nautilus_trader \"Testing\" developer guide for more instructions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup debugging environment - run this cell first\n",
    "from nautilus_trader.test_kit.debug_helpers import setup_debugging\n",
    "\n",
    "\n",
    "# Set up debugging for VS Code (configures launch.json with Jupyter kernel PID)\n",
    "# This creates ALL debugging configurations automatically!\n",
    "# Also starts a debugpy sever the Python debugger can connect to\n",
    "setup_debugging()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## Example: Parquet Catalog Mixed Debugging\n",
    "\n",
    "This example demonstrates mixed Python + Rust debugging using the parquet catalog.\n",
    "We'll write a QuoteTick using `catalog.write_data([quote_tick])` and read it back using `catalog.quote_ticks()`.\n",
    "\n",
    "**Set breakpoints in VS Code at (for example):**\n",
    "- `nautilus_trader/persistence/catalog/parquet.py` : put a breakpoint inside _query_rust\n",
    "- `nautilus_trader/crates/persistence/src/backend/session.rs` : put a breakpoint inside get_query_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "import tempfile\n",
    "\n",
    "# Also works with the usual non-pyo3 InstrumentId, Price, etc.\n",
    "from nautilus_trader.core.nautilus_pyo3 import InstrumentId\n",
    "from nautilus_trader.core.nautilus_pyo3 import Price\n",
    "from nautilus_trader.core.nautilus_pyo3 import Quantity\n",
    "from nautilus_trader.core.nautilus_pyo3 import QuoteTick\n",
    "from nautilus_trader.persistence.catalog import ParquetDataCatalog\n",
    "\n",
    "\n",
    "# Create a temporary directory for our parquet files\n",
    "temp_dir = tempfile.mkdtemp(prefix=\"nautilus_debug_\")\n",
    "print(f\"Created temporary directory: {temp_dir}\")\n",
    "\n",
    "# Initialize parquet catalog\n",
    "catalog = ParquetDataCatalog(temp_dir)\n",
    "\n",
    "instrument_id = InstrumentId.from_str(\"EURUSD.SIM\")\n",
    "quote_tick = QuoteTick(\n",
    "    instrument_id=instrument_id,\n",
    "    bid_price=Price.from_str(\"1.0500\"),\n",
    "    ask_price=Price.from_str(\"1.0505\"),\n",
    "    bid_size=Quantity.from_str(\"100000\"),\n",
    "    ask_size=Quantity.from_str(\"75000\"),\n",
    "    ts_event=1_000_000_000,\n",
    "    ts_init=2_000_000_000,\n",
    ")\n",
    "\n",
    "catalog.write_data([quote_tick])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "read_ticks = catalog.quote_ticks()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "shutil.rmtree(temp_dir)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nautilus",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
